<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="java.sql.*" %>
<%@ page import="bean.DB" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>商品列表</title>
    <style>
        table {
            border-collapse: collapse;
            width: 80%;
            margin: 20px auto;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: center;
        }
        th {
            background-color: #4CAF50;
            color: white;
        }
        tr:nth-child(even) {
            background-color: #f2f2f2;
        }
        tr:hover {
            background-color: #ddd;
        }
        img {
            width: 100px;
            height: 100px;
            object-fit: cover;
        }
    </style>
</head>
<body>
<h1 style="text-align: center;">商品列表</h1>
<table>
    <tr>
        <th>ID</th>
        <th>商品名</th>
        <th>价格</th>
        <th>积分</th>
        <th>商品描述</th>
        <th>图片</th>
        <th>操作</th>
        <th>操作</th>
    </tr>
    <%
        DB db = new DB();
        String sql = "SELECT id, product_name, price, description, image_path, star FROM products";
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            conn = db.createConn();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);

            while (rs.next()) {
                int id = rs.getInt("id");
                String product_name = rs.getString("product_name");
                String price = rs.getString("price");
                String star = rs.getString("star");
                String description = rs.getString("description");
                String imagePath = rs.getString("image_path");
    %>
    <tr>
        <td><%= id %></td>
        <td><%= product_name %></td>
        <td><%= price %></td>
        <td><%= star %></td>
        <td><%= description %></td>
        <td>
            <img src="<%= imagePath %>" alt="商品图片">
        </td>
        <td>
            <form action="./deleteProductServlet" method="POST">
                <input type="hidden" name="id" value="<%= id %>">
                <button type="submit" class="alterUser-button">删除</button>
            </form>
        </td>
        <td>
            <button type="button" onclick="window.location.href='updateProduct.jsp?id=<%= id %>';">修改</button>
        </td>
    </tr>
    <%
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    %>
</table>
</body>
</html>
